package com.papper.common.service.sys;

import com.papper.common.bean.sys.SysGroup;
import com.papper.common.bean.sys.SysLimit;
import com.papper.common.bean.sys.SysUser;
import com.papper.common.enums.LimitTypeEnum;
import com.papper.common.mapper.sys.SysLimitMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.StringTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service("sysLimitService")
public class SysLimitService extends ServiceImpl<SysLimitMapper, SysLimit> {

	/**
	 * function
	 */
	public List<SysLimit> getUserLimits(SysUser sysUser) {
		if (sysUser.getLimitType().intValue() == LimitTypeEnum.GROUP.getCode()) {
			return getGroupLimits(sysUser.getGroupId().longValue());
		} else if (sysUser.getLimitType().intValue() == LimitTypeEnum.USER.getCode()) {
			return getUserLimits(sysUser.getId().longValue(), null, false);
		}
		return null;
	}

	public List<SysLimit> getUserLimits(int type, Long id) {
		if (type == 0) {
			return getGroupLimits(id);
		}
		if (type == 1) {
			return getUserLimits(id, null, false);
		}

		return null;
	}

	public List<SysLimit> getGroupLimits(Long gid) {
		Example example = new Example(SysLimit.class);
		example.createCriteria().andEqualTo("limitType", 0).andEqualTo("limitId", gid);
		return find(example);
	}

	public int limit(Integer limitType, Long limitId, String limitKey) {
		int rtn = getPapperMapper().limit(limitType, limitId, limitKey);
		return rtn;
	}

	public SysLimit getUserLimit(Integer limitType, Long uid, Integer gid, Long resId) {
		Example          example  = new Example(SysLimit.class);
		Example.Criteria criteria = example.createCriteria().andEqualTo("limitType", limitType);
		if (LimitTypeEnum.GROUP.getCode().equals(limitType)) {
			criteria.andEqualTo("limitId", gid);
		} else {
			criteria.andEqualTo("limitId", uid);
		}
		criteria.andEqualTo("resId", resId);
		example.setOrderByClause("id desc limit 1");
		SysLimit rtn = findOne(example);
		return rtn;
	}

	public List<SysLimit> getUserLimits(Long uid, Long gid, boolean bUserGroupLimits) {
		Example example = new Example(SysLimit.class);
		example.createCriteria().andEqualTo("limitType", 1).andEqualTo("limitId", uid);
		List<SysLimit> list = find(example);
		if (list.isEmpty() && bUserGroupLimits) {
			example.clear();
			example.createCriteria().andEqualTo("limitType", 0).andEqualTo("limitId", gid);
			return find(example);
		} else {
			return list;
		}
	}

	public List<SysLimit> getUserLimits(SysUser sysUser, boolean bUserGroupLimits) {
		Example          example  = new Example(SysLimit.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("limitType", 1);
		criteria.andEqualTo("limitId", sysUser.getId());
		List<SysLimit> list = find(example);
		if (list.isEmpty() && bUserGroupLimits) {
			example.clear();
			criteria.andEqualTo("limitType", 0);
			criteria.andEqualTo("limitId", Long.valueOf(sysUser.getGroupId()));
			return find(example);
		} else {
			return list;
		}
	}

	public void updateLimited(Integer limitType, Long limitId, List<SysLimit> limits) {
		for (SysLimit entity : limits) {
			if (entity.getId() == null) {
				save(entity);
			} else {
				update(entity);
			}
		}

		List<SysUser>  childUsers;
		List<SysGroup> childGroups;
		String         strUserIds  = "";
		String         strGroupIds = "";

		// 获取所有子节点用户
	}

	public void delLimitedByGroupIds(String ids) {
		Example          example  = new Example(SysLimit.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("limitType", LimitTypeEnum.GROUP.getCode());
		criteria.andIn("limitId", StringTool.stringToListString(ids, ","));
		delete(example);
	}

	public void delLimitedByResIds(String ids) {
		Example example = new Example(SysLimit.class);
		example.createCriteria().andEqualTo("resIds", ids);
		delete(example);
	}

	public void delLimitedByResIds(Integer limitType, Integer limitId, String ids) {
		Example example = new Example(SysLimit.class);
		example.createCriteria().andEqualTo("limitType", limitType).andEqualTo("limitId", limitId).andIn("id", Arrays.asList(ids.split(",")));
		delete(example);
	}

	public void delLimitedByUserIds(String ids) {
		Example example = new Example(SysLimit.class);
		example.createCriteria().andEqualTo("limitType", LimitTypeEnum.USER.getCode()).andEqualTo("limitId", Arrays.asList(ids.split(",")));
		delete(example);
	}

	public void delAllNullLimits() {
		Example example = new Example(SysLimit.class);
		example.createCriteria().andEqualTo("limitValue", 0);
		delete(example);
	}
}